package com.bookshop.promotion.logic;

import java.util.Date;

import com.bookshop.pojo.PromotionItemPOJO;
import com.bookshop.promotion.data.CreditsPair;
import com.bookshop.tool.Deformat;
import com.bookshop.vo.PromotionItemVO;

public class POJOsToVO {

	static PromotionItemVO pojosToVo(PromotionItemPOJO[] pPromotionPOJOs) {
		PromotionItem[] promotionItems = pojoToItems(pPromotionPOJOs);
		PromotionItemVO vo = new PromotionItemVO();
		vo.update(promotionItems);
		return vo;
	}
	
	static PromotionItem[] pojoToItems(PromotionItemPOJO[] pPromotionItemPOJOs){
		PromotionItem[] promotionItems = new PromotionItem[pPromotionItemPOJOs.length];
		for (int i = 0; i < pPromotionItemPOJOs.length; i++) {
			promotionItems[i] = POJOtoItem(pPromotionItemPOJOs[i]);
		}
		return promotionItems;
	}

	static PromotionItem POJOtoItem(PromotionItemPOJO promotionItemPOJO) {
		if (promotionItemPOJO == null) {
			return null;
		}
		Date date = Deformat.date(promotionItemPOJO.mDate);
		DiscountCoupons discountCoupons = Deformat.singleDiscCoupons(promotionItemPOJO.mDiscountCoupons);
		EqualCoupons equalCoupons = Deformat.singleEqualCoupons(promotionItemPOJO.mEqualCoupons);
		CreditsPair creditsPair = Deformat.deformatGrades(promotionItemPOJO.mCredits);
		return new PromotionItem(promotionItemPOJO.mID, date, creditsPair.mMin, creditsPair.mMax, discountCoupons, equalCoupons);
	}
}
